package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderListService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/order")
@CrossOrigin
public class OrderListController {

    @Reference
    private OrderListService orderListService;

    @GetMapping("/findPage")
    public Result findPage(OrderQueryPageBean orderQueryPageBean){
        try {
            PageResult pageResult = orderListService.pageQuery(
                    orderQueryPageBean.getCurrentPage(),
                    orderQueryPageBean.getPageSize(),
                    orderQueryPageBean);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @PostMapping("/submit")
    public Result add(@RequestBody Order order){
        try {
            orderListService.add(order);
            return new Result(true,MessageConstant.ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")String id){
        try {
            Order order = orderListService.pageById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @PostMapping("/updataOrderStatus")
    public Result updataOrderStatus(@RequestBody Order order){
        try {
            orderListService.updataOrderStatus(order);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
